package com.querie.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.querie.model.beans.Fragment;
import com.querie.model.beans.QueryLog;
import com.querie.sql.BaseDAO;
import com.querie.sql.PreparedStatementSetter;
import com.querie.sql.ResultSetExtractor;

public  class FragmentDAOImpl extends BaseDAO implements FragmentDAO{
	public Fragment load(final long id) throws Exception{
		String sql = "SELECT * FROM fragment WHERE fragmentid=?";
		return (Fragment) getRow(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setLong(1, id);
			}
		}, new ResultSetExtractor() {
			public Object extract(ResultSet rs) throws SQLException {
			Fragment row  = new Fragment();
				row.setFragmentId(rs.getLong("fragmentid"));
				row.setFragment(rs.getString("fragment"));
				row.setFragmentType(rs.getString("fragmenttype"));
				return row;
			}
	});
	}

	public Fragment loadFromFragment(final String fragment) throws Exception{
		String sql = "SELECT * FROM fragment WHERE fragment=?";
		return (Fragment) getRow(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setString(1, fragment);
			}
		}, new ResultSetExtractor() {
			public Object extract(ResultSet rs) throws SQLException {
			Fragment row  = new Fragment();
				row.setFragmentId(rs.getLong("fragmentid"));
				row.setFragment(rs.getString("fragment"));
				row.setFragmentType(rs.getString("fragmenttype"));
				return row;
			}
	});
	}
	
	public int delete(final long id) throws Exception{
		String sql = "DELETE FROM fragment WHERE fragmentid=?";
		return executeUpdate(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setLong(1, id);
			}
		});
	}

	public int update(final Fragment data) throws Exception{
		String sql = "UPDATE fragment SET fragment = ?, fragmenttype = ? WHERE fragmentid=?";
		return executeUpdate(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setString(1, data.getFragment());
				ps.setString(2, data.getFragmentType());
				ps.setLong(3, data.getFragmentId());
			}
	});
	}

	public long insert(final Fragment data) throws Exception{
		
		Fragment frag = loadFromFragment(data.getFragment());
		
		if (frag == null)
		{
			String sql = "INSERT INTO fragment ( fragment, fragmenttype) VALUES (?, ? )";
		
			long id = executeInsert(sql, new PreparedStatementSetter() {
				public void prepare(PreparedStatement ps) throws SQLException {
					
					ps.setString(1, data.getFragment());
					ps.setString(2, data.getFragmentType());
				}
			});
			data.setFragmentId(id);
			return id;
		}
		else
		{
			return frag.getFragmentId();
		}
	}
	public int deleteAll() throws Exception{
		String sql = "DELETE FROM fragment";
		return executeUpdate(sql, null);
	}
	@SuppressWarnings("unchecked")
	public List<Fragment> queryAll()throws Exception{
		String sql = "SELECT * FROM fragment";
		return (List<Fragment>) getList(sql, new ResultSetExtractor() {
			public Object extract(ResultSet rs) throws SQLException {
				Fragment row = new Fragment();
				row.setFragmentId(rs.getLong("fragmentid"));
				row.setFragment(rs.getString("fragment"));
				row.setFragmentType(rs.getString("fragmenttype"));
				return row;
			}
	});
	}
	
	public List<Fragment> queryAllWithPage(int startOfPage, int...pageSize)throws Exception{
		
		int page = 20;
		if (pageSize.length > 0)
		{
			page = pageSize[0];
		}
		String sql = "SELECT * FROM fragment LIMIT "+startOfPage+" ,"+page ;
		@SuppressWarnings("unchecked")
		List<Fragment> list = ((List<Fragment>) getList(sql, new ResultSetExtractor() {
			public Object extract(final ResultSet rs) throws SQLException {
				final Fragment row = new Fragment();
				row.setFragmentId(rs.getLong("fragmentid"));
				row.setFragment(rs.getString("fragment"));
				row.setFragmentType(rs.getString("fragmenttype"));
				return row;
			}
	}));
		return list;
	}


	 public ResultSetExtractor getListResultSetExtractor(){
		return new ResultSetExtractor() {
			public Object extract(ResultSet rs) throws SQLException {
				Fragment row = new Fragment();
				row.setFragmentId(rs.getLong("fragmentid"));
				row.setFragment(rs.getString("fragment"));
				row.setFragmentType(rs.getString("fragmenttype"));
				return row;
			}
	};
	}
	 
	public int alterTable() throws Exception
	{
		String sql = "alter table fragment auto_increment=0";
		return executeUpdate(sql, null);

	}
	 
	public static void main(String a[])
	{
		FragmentDAOImpl d = new FragmentDAOImpl();
		
		try {
			d.alterTable();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}